package com.celi.scs.common.constant.utils;

import com.celi.system.entity.Dept;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @project: bg-scs-template
 * @title: DeptUtil
 * @author: lihq
 * @date: 2024/7/12 15:53
 * @version: v1.0
 * @description: 部门工具类
 */
public class DeptUtil {

    /**
     * 递归设置子机构
     * @param dept     当前传入的机构
     * @param deptList      所有机构
     * @return
     */
    public static List<Dept> recursion(Dept dept, List<Dept> deptList) {
        List<Dept> list = deptList.stream()
                //1. 过滤
                .filter(d -> StringUtils.isNotBlank(d.getParentId()) && d.getParentId().equals(dept.getDeptId()))
                .map(d -> {
                    d.setChildren(recursion(d, deptList));
                    return d;
                })
                .sorted((dept1, dept2) -> {
                    //2. 机构排序
                    return (dept1.getOrderIndex() == null ? 0 : dept1.getOrderIndex()) - (dept2.getOrderIndex() == null ? 0 : dept2.getOrderIndex());
                })
                .collect(Collectors.toList());
        return list;
    }

}
